<?php
session_start();

if (isset($_POST['submitEdit'])) {

    $title = $_POST['actTitle'];
    $desc = $_POST['actDesc'];
    $date = $_POST['actDate'];
    $id = $_POST['id'];

    include 'DBLibrary/config.php';
    include 'DBLibrary/openDB.php';

    mysql_select_db("rotaract");


    $query = "SELECT * FROM activity WHERE id='$id'";
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result);

    define('FileFolder', 'activityImages/');

    if (isset($_POST['submitEdit'])) {
        $dafile = $_FILES['imageUpload']['name'];
        $target = FileFolder . $dafile;
        if (strcmp($target, $row['imageurl']) != 0) {
            move_uploaded_file($_FILES['imageUpload']['tmp_name'], $target);
        }
    }
    if (strcmp($target, "activityImages/") == 0) {
        $result = mysql_query("UPDATE `rotaract`.`activity` SET `title`= '$title', `date`= '$date', `desc`= '$desc' WHERE `activity`.`id`= $id") or die(mysql_error());
    }
    else
        $result = mysql_query("UPDATE `rotaract`.`activity` SET `title`= '$title', `date`= '$date', `desc`= '$desc', `imageurl`= '$target' WHERE `activity`.`id`= $id") or die(mysql_error());

    $confirm = 'Update successful!';
}
?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Rotaract DLSU</title>
        <meta http-equiv="Content-Type" content="text/html"/>
        <meta name="robots" content="index,follow"/>
        <meta name="distribution" content="global"/>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>
        <link href="css/style.css" rel="stylesheet" type="text/css" media="screen" />
        <link rel="stylesheet" href="css/jquery-ui-1.8.21.custom.css"/>
        <link rel="stylesheet" href="css/demos.css"/>
        <script src="js/jquery-1.7.2.min.js"></script>
        <script src="js/jquery.ui.core.js"></script>
        <script src="js/jquery.ui.widget.js"></script>
        <script src="js/jquery.ui.datepicker.js"></script>

        <script>
            $(function() {
                $( "#datepicker" ).datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'yy-mm-dd'
                });
            });
        </script>

    </head>
    <body>
        <div id="container2">
            <div id="main2">
                <table width="100%" cellspacing="0" cellpadding="0">
                    <tr>
                        <td class="main_nav">
                            <div id="main_text2"> 
                                <center>
                                    <h2>Edit an Activity</h2>
                                    <form enctype = "multipart/form-data" name="imageUpload" action="editActivity.php" method="POST" >
                                        <?
                                        include 'DBLibrary/config.php';
                                        include 'DBLibrary/openDB.php';

                                        $editName = $_POST['actTitle'];

                                        mysql_select_db("rotaract");
                                        $query = "SELECT * FROM activity WHERE title='$editName'";
                                        $result = mysql_query($query) or die(mysql_error());

                                        if (isset($confirm)) {
                                            echo "<br/>";
                                            echo "<h3>$confirm</h3>";
                                        }
                                        ?>
                                        <table border ="1" cellpadding="10" class="myTable">
                                            <br/>
                                            <br/>

                                            <?
                                            while ($row = mysql_fetch_array($result)) {
                                                $id = $row['id'];
                                                $title = $row['title'];
                                                $date = $row['date'];
                                                $desc = $row['desc'];
                                                $img = $row['imageurl'];
                                                echo "<input type='hidden' name='id' value='" . $id . "'/>";
                                                if ($img != '') {
                                                    echo ' <tr>';
                                                    echo '        <td><b>Name of Activity</b></td>';
                                                    echo '     <td><input type="text" name="actTitle" value="' . $title . '"/></td>';
                                                    echo '</tr>';
                                                    echo ' <tr>';
                                                    echo '        <td><b>Date</b></td>';
                                                    echo '     <td><input type="text" id="datepicker" name="actDate" value="' . $date . '"/></td>';
                                                    echo '</tr>';
                                                    echo ' <tr>';
                                                    echo '        <td><b>Description</b></td>';
                                                    echo '     <td><input type="text" name="actDesc" value="' . $desc . '"/></td>';
                                                    echo '</tr>';
                                                    echo ' <tr>';
                                                    echo '        <td><b>Image</b></td>';
                                                    echo '     <td><img src=' . $img . ' height="50" width="50"><input type="file" name="imageUpload"/></td>';
                                                    echo '</tr>';
                                                } else {
                                                    echo ' <tr>';
                                                    echo '        <td><b>Name of Activity</b></td>';
                                                    echo '     <td><input type="text" name="actTitle" value="' . $title . '"/></td>';
                                                    echo '</tr>';
                                                    echo ' <tr>';
                                                    echo '        <td><b>Date</b></td>';
                                                    echo '     <td><input type="text" id="datepicker" name="actDate" value="' . $date . '"/></td>';
                                                    echo '</tr>';
                                                    echo ' <tr>';
                                                    echo '        <td><b>Description</b></td>';
                                                    echo '     <td><input type="text" name="actDesc" value="' . $desc . '"/></td>';
                                                    echo '</tr>';
                                                    echo ' <tr>';
                                                    echo '        <td><b>Image</b></td>';
                                                    echo '     <td>No image available.</td>';
                                                    echo '</tr>';
                                                }
                                            }
                                            ?>
                                        </table>
                                        <br/>
                                        <br/>

                                        <input type="submit" name="submitEdit" value="Save" onclick="confirm('Are you sure these values are correct?')"/>
                                    </form>
                                </center>
                            </div>
                        </td>

                    </tr>
                </table>
            </div>
        </div>
    </body>
</html>

